#!/usr/bin/env python
# -*- coding: iso-8859-2 -*-
#
from __future__ import division
from math import log

def log2( x ):
    """
    Zwraca log2

    @access public
    @param float
    @return float
    """
    return log(x)/log(2.0)

def histogramEqualization(data , size,maxColorCount= 256):
	hist = data.getcolors()
	colCount = len(hist)

	dyst =	[0] * maxColorCount 
	lut = [0] * maxColorCount 
	maxColorCount = maxColorCount - 1

	dyst[hist[0][1]] = hist[0][0]/size
	d0 = float(dyst[hist[0][1]])
	oned0 = float(1 - d0) 
	sum = hist[0][0]

	for color in hist[1:]:
		sum += color[0]
		dyst[color[1]] = sum/size 
		lut[color[1]] = int(((dyst[color[1]] - d0)/(oned0))*maxColorCount)

	return data.point(lut)

